package controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import domain.Clothes;
import domain.Order;
import domain.User;
import domain.UserPower;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import service.UserService;
import utils.DateGridViewResult;
import vo.Clothesvo;
import vo.Uservo;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName : UserController
 * @Description : //用户管理类
 * @Author : LYH
 * @Date: 2022/5/27  19:53
 */
@Controller
public class UserController {
    @Autowired
    private UserService userService;
//用户登录方法
    @RequestMapping("/login")
    @ResponseBody
    public Map<String, Object> login(User user, HttpSession session)  {
            Map<String, Object> map = new HashMap<>();
            
            User user1 = userService.findUserByusername(user.getUsername());
        System.out.println(user1);
            if (user1==null){
                map.put("result", false);
            }
            else {
                if (user.getUsername().equals(user1.getUsername()) && user.getPassword().equals(user1.getPassword())) {
                    //登录成功，将用户信息存入到session中
                    UserPower userPower= userService.findUserPower(user1);
                    session.removeAttribute("userpower");
                    System.out.println(userPower);

                    session.setAttribute("userpower",userPower);
                    System.out.println(session.getAttribute("userpower"));

                    session.removeAttribute("user");
                    session.setAttribute("user",user1);

                    map.put("result", true);
                } else {
                    map.put("result", false);
                }
            }

            return map;

    }
//用户注册方法
    @RequestMapping("/register")
    @ResponseBody
    public Map<String, Object> register(User user){
        Map<String, Object> map = new HashMap<>();
        //判断用户名是否已存在
        User user1=userService.findUserByusername(user.getUsername());
        if(user1==null){
            int flag=userService.insertUser(user);
            map.put("result",true);
            return map;
        }
        else {
            map.put("result",false);
            return map;
        }

    }
//用户修改方法
    @RequestMapping("/modify")
    @ResponseBody
    public Map<String,Object> modify(User user,HttpSession session){
        Map<String, Object> map = new HashMap<>();
        User user1= (User) session.getAttribute("user");
        //将用户信息设置为前端填写的值
        user1.setUsername(user.getUsername());
        user1.setPassword(user.getPassword());
        int flag=userService.updateUser(user1);
        if(flag>=0){
            map.put("result",true);
        }else{
            map.put("result",false);
        }

        System.out.println(map.get("result"));

        return map;
    }
//用户查找方法
    @RequestMapping("/user_search")
    @ResponseBody
    public DateGridViewResult user_search(Uservo uservo){
        System.out.println(uservo.getId());
        PageHelper.startPage(uservo.getPage(),uservo.getLimit());

        List<UserPower> userPowerList=userService.findUserList(uservo);
        PageInfo<UserPower> pageInfo = new PageInfo<>(userPowerList);

        return new DateGridViewResult(pageInfo.getTotal(),pageInfo.getList());
    }
//更新用户权限方法
    @RequestMapping("/user_updatemanage")
    @ResponseBody
    public Map<String,Object> user_updatemanage(UserPower userPower){
        Map<String,Object> map=new HashMap<>();
        System.out.println(userPower.toString());
        int flag=userService.user_updatemanage(userPower);
        if(flag>0){
            map.put("result",true);
        }else {
            map.put("result",false);
        }

        return map;
    }
    //删除用户
    @RequestMapping("/user_delete")
    @ResponseBody
    public Map<String,Object> user_delete(UserPower userPower){
        Map<String,Object> map=new HashMap<>();
        System.out.println(userPower.toString());
        int flag=userService.deleteUser(userPower);
        if(flag>0){
            map.put("result",true);
        }else {
            map.put("result",false);
        }

        return map;
    }
    //批量删除方法
    @RequestMapping("/user_deleteList")
    @ResponseBody
    public Map<String,Object> userDeleteList(@RequestBody List<UserPower> userPowerList){
        Map<String,Object> map=new HashMap<>();
        int flag=0;


        for (UserPower userPower : userPowerList) {
            System.out.println(userPower.toString());
            flag=userService.deleteUser(userPower);
        }
        if(flag>0){
            map.put("result",true);
        }else {
            map.put("result",false);
        }

        return map;
    }

}
